package com.onstar.cn.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.weight.anim.HomeLoadingLayout;

/**
 * Created by 6335yz on 2016/7/5.
 */
public class HomeBallViewNew extends RelativeLayout {

    private View view, login_layout, status_layout;
    private TextView tv_status, tv_status_msg, tv_login;
    private ImageButton ball_login, ball_status;
    private HomeLoadingLayout login_loading_ll;

    public HomeBallViewNew(Context context) {
        super(context);
        init(context);
    }

    public HomeBallViewNew(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HomeBallViewNew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.weight_home_ball, null);
        addView(view);
        findView();
    }

    private void findView() {
        login_layout = view.findViewById(R.id.login_layout);
        status_layout = view.findViewById(R.id.status_layout);
        tv_status = (TextView) view.findViewById(R.id.tv_status);
        tv_status_msg = (TextView) view.findViewById(R.id.tv_status_msg);
        tv_login = (TextView) view.findViewById(R.id.tv_login);
        ball_login = (ImageButton) view.findViewById(R.id.ball_login);
        ball_status = (ImageButton) view.findViewById(R.id.ball_status);
        login_loading_ll = (HomeLoadingLayout) view.findViewById(R.id.login_loading_ll);
        login_loading_ll.setVisibility(View.GONE);

//        HomeLoadingLayout login_loading_lll = (HomeLoadingLayout) view.findViewById(R.id.login_loading_lll);
//        login_loading_lll.startAnim();
        showLoginLayout(getResources().getString(R.string.ball_login));
    }

    public void setLoginListener(OnClickListener listener) {
        ball_login.setOnClickListener(listener);
    }

    public void setStatusListener(OnClickListener listener) {
        if(login_loading_ll!=null) {
            login_loading_ll.stopAnim();
        }

        ball_status.setOnClickListener(listener);
    }

    public void showLoginLayout(String loginMsg) {
        login_loading_ll.setVisibility(View.GONE);
        ball_status.setEnabled(false);
        status_layout.setVisibility(GONE);
        ball_login.setEnabled(true);
        login_layout.setVisibility(VISIBLE);
        ball_login.setBackgroundResource(R.drawable.weight_ball_login);
        tv_login.setText(loginMsg);
    }

    public void disableLoginLayout(String loginMsg) {
        ball_login.setEnabled(false);
        tv_login.setText(loginMsg);
    }

    public void showStatusLayout(int res, String status, String statusMsg) {
//        ball_login.setEnabled(false);
        login_loading_ll.setVisibility(View.GONE);
        login_layout.setVisibility(GONE);
        ball_status.setEnabled(true);
        status_layout.setVisibility(VISIBLE);
        ball_status.setImageResource(res);
        if (CommonUtil.isNull(status)) {
            tv_status.setVisibility(View.GONE);
        } else {
            tv_status.setVisibility(View.VISIBLE);
            tv_status.setText(status);
        }
        tv_status_msg.setText(statusMsg);
//        startAnimation(tv_status,tv_status_msg);
    }
    private void startAnimation(View... view) {
        AnimationSet set = new AnimationSet(true);
        Animation alphaAnimation = new AlphaAnimation(0f,1f);
        alphaAnimation.setDuration(300);
        set.addAnimation(alphaAnimation);
        Animation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scaleAnimation.setDuration(300);
        set.addAnimation(scaleAnimation);
        if(view !=null && view.length>0) {
            for(View v: view) {
                v.startAnimation(set);
            }
        }
    }
//    public void showVisitorLayout(int res, String status, String statusMsg) {
////        ball_login.setEnabled(false);
//        login_loading_ll.setVisibility(View.GONE);
//        login_layout.setVisibility(GONE);
//        ball_status.setEnabled(true);
//        status_layout.setVisibility(VISIBLE);
////        ball_status.setBackgroundResource(res);
//        if (CommonUtil.isNull(status)) {
//            tv_status.setVisibility(View.GONE);
//        } else {
//            tv_status.setVisibility(View.VISIBLE);
//            tv_status.setText(status);
//        }
//        tv_status_msg.setText(statusMsg);
//    }

    public void showAutoLogin() {
        showStatusLayout(R.drawable.weight_ball_login,getResources().getString(R.string.ball_home_login_ing),"");
        ball_status.setEnabled(false);
        status_layout.setVisibility(View.VISIBLE);
        login_loading_ll.setVisibility(View.VISIBLE);
        login_loading_ll.startAnim();
    }
}
